/**
 * DrawerEdge.java
 *
 * Created on Feb 8, 2007
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.ui.drawer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import javax.swing.JComponent;
import javax.swing.UIManager;

/**
 * This component is used to represent the "edge" of a <CODE>Drawer</CODE>.  It has a graphical
 * handle to remind the user that it's an interactive component, and it can be clicked on
 * by the user to open or close the <CODE>Drawer</CODE>.
 *
 * @author Brandon Franklin
 */
class DrawerEdge extends JComponent {

    static final private long serialVersionUID = 587044260814796692L;

    /** The <CODE>Drawer</CODE> that is using this edge */
    final private Drawer drawer;

    /**
     * Constructs a new <CODE>DrawerEdge</CODE> and prepares it for use.
     *
     * @param drawer the <CODE>Drawer</CODE> that is using this edge
     */
    DrawerEdge( final Drawer drawer ) {
        this.drawer = drawer;

        addMouseListener( new DrawerEdgeMouseListener( drawer ) );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension getPreferredSize() {

        final Dimension size = new Dimension();
        if( (drawer.getOrientation() == DrawerOrientation.LEFT) || (drawer.getOrientation() == DrawerOrientation.RIGHT) ) {
            size.setSize( 10, Integer.MAX_VALUE );
        } else {
            size.setSize( Integer.MAX_VALUE, 10 );
        }
        return size;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void paintComponent( final Graphics g ) {

        final Graphics2D g2 = (Graphics2D)g.create();

        final Color highlight = UIManager.getLookAndFeelDefaults().getColor( "SplitPane.highlight" );
        final Color shadow = UIManager.getLookAndFeelDefaults().getColor( "SplitPane.darkShadow" );

        g2.setColor( drawer.getBackground() );
        g2.fillRect( 0, 0, getWidth(), getHeight() );

        // Values used for positioning the "handle"
        final int centerX = getWidth() / 2;
        final int centerY = getHeight() / 2;
        final int diam = Math.min( getWidth(), getHeight() ) / 2;
        final int radius = diam / 2;
        final Point[] handlePoints = new Point[]{
            new Point( centerX - radius, centerY - radius ),
            new Point( centerX - radius, centerY - radius ),
            new Point( centerX - radius, centerY - radius )
        };

        switch( drawer.getOrientation() ) {
            case LEFT:
            case RIGHT:
                g2.setColor( highlight );
                g2.drawLine( 0, 0, 0, getHeight() );

                g2.setColor( shadow );
                g2.drawLine( getWidth() - 1, 0, getWidth() - 1, getHeight() );

                handlePoints[1].translate( 0, -diam * 2 );
                handlePoints[2].translate( 0, diam * 2 );
                break;

            case TOP:
            case BOTTOM:
                g2.setColor( highlight );
                g2.drawLine( 0, 0, getWidth(), 0 );

                g2.setColor( shadow );
                g2.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );

                handlePoints[1].translate( -diam * 2, 0 );
                handlePoints[2].translate( diam * 2, 0 );
                break;
        }

        // Draw the "handle"
        g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
        for( final Point point : handlePoints ) {
            g2.setColor( highlight );
            g2.drawOval( point.x, point.y, diam, diam );

            g2.setColor( shadow );
            g2.drawOval( point.x - 1, point.y - 1, diam, diam );
        }
    }
}
